﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace DarkLordsApprenticeV2
{
    class Fire : DrawableGameComponent
    {


        public Fire(Game g, Vector2 position, int evilLevel, int firePower)
            : base(g)
        {
            Position = position;
            index = 0;
            animCounter = 0;
            frames = new Texture2D[3][];
            frames[0] = new Texture2D[2];
            frames[1] = new Texture2D[2];
            frames[2] = new Texture2D[2];
            if (evilLevel < 4)
            {
                state = State.Red;
                intensity = firePower*.8f;
            }
            else if (evilLevel < 7)
            {
                state = State.Blue;
                intensity = firePower*1f;
            }
            else
            {
                state = State.Black;
                intensity = firePower*1.2f;
            }
            scale = (firePower / 100f) * (Position.Y / 590);
            
        }

        int index;
        int animCounter;
        Texture2D[][] frames;
        Texture2D image;
        SpriteBatch spriteBatch;
        public Vector2 Position { get; set; }
        public float scale;

        enum State { Red, Blue, Black };
        State state;

        float intensity;
        Boolean dead = false;

        protected override void LoadContent()
        {
            frames[0][0] = Game.Content.Load<Texture2D>("RedFire1");
            frames[0][1] = Game.Content.Load<Texture2D>("RedFire2");
            frames[1][0] = Game.Content.Load<Texture2D>("BlueFire1");
            frames[1][1] = Game.Content.Load<Texture2D>("BlueFire2");
            frames[2][0] = Game.Content.Load<Texture2D>("BlackFire1");
            frames[2][1] = Game.Content.Load<Texture2D>("BlackFire2");

            spriteBatch = new SpriteBatch(Game.GraphicsDevice);

            switch (state)
            {
                case State.Red:
                    image = frames[0][0];
                    break;

                case State.Blue:
                    image = frames[1][0];
                    break;

                case State.Black:
                    image = frames[2][0];
                    break;
            }
        }

        public override void Update(GameTime gameTime)
        {
            int stateIndex = 0;
            switch (state)
            {
                case State.Red:
                    stateIndex = 0;
                    break;

                case State.Blue:
                    stateIndex = 1;
                    break;

                case State.Black:
                    stateIndex = 2;
                    break;
            }

            if (animCounter % 10 == 0)
            {
                index++;
                if (index >= frames[stateIndex].Length)
                {
                    index = 0;
                }
                image = frames[stateIndex][index];

                scale -= .05f;
                if (scale <= 0)
                {
                    scale = 0;
                    dead = true;
                }

                intensity--;
            }

            animCounter++;
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();
            //spriteBatch.Draw(frame, Position, Color.White);
            //spriteBatch.Draw(image, Position, Color.White);
            spriteBatch.Draw(image, Position, null, Color.White, 0, new Vector2(image.Width / 2, image.Height), scale, SpriteEffects.None, 0);
            spriteBatch.End();
        }

        public void SwitchToRed()
        {
            state = State.Red;
            animCounter = 0;
            index = 0;
        }

        public void SwitchToBlue()
        {
            state = State.Blue;
            animCounter = 0;
            index = 0;
        }

        public void SwitchToBlack()
        {
            state = State.Black;
            animCounter = 0;
            index = 0;
        }

        public float GetIntensity()
        {
            return intensity;
        }

        public float GetWidth()
        {
            return image.Width * scale;
        }

        public Boolean IsDead()
        {
            return dead;
        }
    }
}